﻿using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;

namespace CPETwit.WPFClient.UC
{
    /// <summary>
    /// Logique d'interaction pour CustomPasswordBox.xaml
    /// </summary>
    public partial class CustomPasswordBox : UserControl
    {
        public static readonly DependencyProperty PasswordProperty =
            DependencyProperty.Register("Password", typeof(string), typeof(CustomPasswordBox),
            new PropertyMetadata(string.Empty, PasswordPropertyChangedCallback));

        private static void PasswordPropertyChangedCallback(DependencyObject dependencyObject,
            DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            CustomPasswordBox customPasswordBox = dependencyObject as CustomPasswordBox;
            if (customPasswordBox == null) return;
            string text = (string)dependencyPropertyChangedEventArgs.NewValue;
            if (string.IsNullOrEmpty(text))
            {
                customPasswordBox.passwordBox.Password = text;
                customPasswordBox.UpdateTextBlockVisibility();
            }
            else if (text != customPasswordBox.passwordBox.Password)
                customPasswordBox.passwordBox.Password = text;
        }

        public string DefaultText
        {
            get { return txtblDefaultText.Text; }
            set { txtblDefaultText.Text = value; }
        }

        public string Password
        {
            get { return (string)GetValue(PasswordProperty); }
            set
            {
                SetValue(PasswordProperty, value);
            }
        }

        public CustomPasswordBox()
        {
            InitializeComponent();

            passwordBox.GotFocus += passwordBox_GotFocus;
            passwordBox.LostFocus += new RoutedEventHandler(passwordBox_LostFocus);
            txtblDefaultText.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(txtblDefaultText_MouseLeftButtonDown);

            GotFocus += new RoutedEventHandler(CustomPasswordBox_GotFocus);
            LostFocus += new RoutedEventHandler(CustomPasswordBox_LostFocus);
        }

        void CustomPasswordBox_LostFocus(object sender, RoutedEventArgs e)
        {
            Password = passwordBox.Password;
        }

        void txtblDefaultText_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            passwordBox.Focus();
        }

        void CustomPasswordBox_GotFocus(object sender, RoutedEventArgs e)
        {
            passwordBox.Focus();
        }

        void passwordBox_LostFocus(object sender, RoutedEventArgs e)
        {
            UpdateTextBlockVisibility();
        }

        void passwordBox_GotFocus(object sender, RoutedEventArgs e)
        {
            if (txtblDefaultText.IsVisible)
                txtblDefaultText.Visibility = Visibility.Collapsed;
        }

        private void UpdateTextBlockVisibility()
        {
            txtblDefaultText.Visibility = passwordBox.Password.Any()
                ? Visibility.Collapsed : Visibility.Visible;
        }

    }
}
